package schooltest;

import static org.junit.Assert.*;
import java.util.Date;

import org.junit.Before;
import org.junit.Test;

import school.DateHelper;
import school.Student;
import school.course.Course;
import schooltest.SimpleCourse;

public class TestStudent {

	Date future = DateHelper.getDateFromToday(5);
	Date past = DateHelper.getDateFromToday(-5);
	Date now = DateHelper.getToday();
	
	Course courseNow ;
	Course coursePast ;
	Course courseFuture ;
	
	Student student ;
	
	@Before
	public void setUp() throws Exception {
		student = new Student();
		
		courseNow = new SimpleCourse();
		coursePast = new SimpleCourse();
		courseFuture = new SimpleCourse();
	}

	public TestStudent() {
	}
	
	private void addAllCourses(){
		student.addCourse(courseNow);
		student.addCourse(coursePast);
		student.addCourse(courseFuture);
	}
	
	@Test
	public void testCurrentCourse() {
		addAllCourses();
		courseNow.setStartDate(now);
		courseNow.setEndDate(now);
		courseFuture.setStartDate(DateHelper.getDateFromToday(1));
		courseFuture.setEndDate(null);
		coursePast.setStartDate(DateHelper.getDateFromToday(-2));
		coursePast.setEndDate(DateHelper.getDateFromToday(-1));
		assertEquals(1, student.getCurrentCourses().length);
		assertEquals(courseNow, student.getCurrentCourses()[0]);
		
		courseNow.setStartDate(past);
		courseNow.setEndDate(future);
		assertEquals(1, student.getCurrentCourses().length);
		assertEquals(courseNow, student.getCurrentCourses()[0]);
		
		courseNow.setStartDate(past);
		courseNow.setEndDate(null);
		assertEquals(1, student.getCurrentCourses().length);
		assertEquals(courseNow, student.getCurrentCourses()[0]);

		courseNow.setStartDate(null);
		courseNow.setEndDate(null);
		assertEquals(1, student.getCurrentCourses().length);
		assertEquals(courseNow, student.getCurrentCourses()[0]);
	}

	@Test
	public void testGetFutureCourses() {
		addAllCourses();
		courseFuture.setStartDate(future);
		courseFuture.setEndDate(null);
		courseNow.setStartDate(past);
		courseNow.setEndDate(future);
		coursePast.setStartDate(past);
		coursePast.setEndDate(future);
		assertEquals(1, student.getFutureCourses().length);		
		assertEquals(courseFuture, student.getFutureCourses()[0]);
	}
	
	@Test
	public void testAddCourse() {
		student.addCourse(courseNow);
		assertEquals(1, student.getCourses().length);
		assertEquals(courseNow, student.getCourses()[0]);
		assertEquals(1, courseNow.getStudents().length);
		assertEquals(student, courseNow.getStudents()[0]);
		
		student.addCourse(courseFuture);
		assertEquals(2, student.getCourses().length);
		assertEquals(courseNow, student.getCourses()[0]);
		assertEquals(courseFuture, student.getCourses()[1]);
		assertEquals(1, courseNow.getStudents().length);
		assertEquals(student, courseNow.getStudents()[0]);
		assertEquals(1, courseFuture.getStudents().length);
		assertEquals(student, courseFuture.getStudents()[0]);

		student.addCourse(courseFuture);
		assertEquals(2, student.getCourses().length);
		assertEquals(courseNow, student.getCourses()[0]);
		assertEquals(courseFuture, student.getCourses()[1]);
		assertEquals(1, courseNow.getStudents().length);
		assertEquals(student, courseNow.getStudents()[0]);
		assertEquals(1, courseFuture.getStudents().length);
		assertEquals(student, courseFuture.getStudents()[0]);
	}
	
	@Test
	public void testRemoveCourse() {
		student.addCourse(courseNow);
		student.addCourse(courseFuture);
		student.removeCourse(courseFuture);
		assertEquals(1, student.getCourses().length);
		assertEquals(courseNow, student.getCourses()[0]);
		
		student.removeCourse(courseFuture);
		assertEquals(1, student.getCourses().length);
		assertEquals(courseNow, student.getCourses()[0]);
		
		student.removeCourse(courseNow);
		assertEquals(0, student.getCourses().length);
	}
}
